// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import BaseView from "../base/BaseView";
import PlateListComponent from "../base/components/PlateListComponent";
import TushareModule from "../module/TushareModule";
import { PlateEnumType, ThsRankData } from "./models/ThsRankData";

const {ccclass, property} = cc._decorator;

@ccclass
export default class SelectPlateView extends BaseView {

    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Prefab)
    plateListPrefab: cc.Prefab = null;

    @property(cc.EditBox)
    editbox: cc.EditBox = null;

    @property(cc.Node)
    confirmNode: cc.Node = null;

    plateListComps: PlateListComponent[] = [];

    _callback: any = null;

    _isSingle: boolean = false;

    onLoad () {
        let names = ['行业', '细分行业', '概念'];
        for (let index = 0; index < names.length; index++) {
            let node = cc.instantiate(this.plateListPrefab);
            this.content.addChild(node);
            let comp = node.getComponent(PlateListComponent);
            this.plateListComps.push(comp);
        }
        cc.director.on('SelectPlateEvent', this.onSelectPlateEvent, this);
    }

    protected onDestroy(): void {
        cc.director.off('SelectPlateEvent', this.onSelectPlateEvent, this);
    }

    initView () {
        let names = ['行业', '细分行业', '概念'];
        let industry = ThsRankData.getInstance().getPlatelist(PlateEnumType.Industry);
        let subtrade = ThsRankData.getInstance().getPlatelist(PlateEnumType.Subtrade); 
        let concept = ThsRankData.getInstance().getPlatelist(PlateEnumType.Concept); 
        let lists = [industry, subtrade, concept];
        for (let index = 0; index < names.length; index++) {
            const name = names[index];
            const list = lists[index];
            let comp = this.plateListComps[index];
            comp.setData(list, name, !this._isSingle);
        }
    }

    show (params) {
        super.show(params);
        let selects = params.selects;
        this._callback = params.callback;
        this._isSingle = params.isSingle;
        this.initView();
        if (this._isSingle) {
            this.confirmNode.active = false;
        } else {
            // for (let index = 0; index < this.plateListComps.length; index++) {
            //     const comp = this.plateListComps[index];
            //     comp.setSelect(selects);
            // }
            this.confirmNode.active = true;
        }
    }

    onSelectPlateEvent (itemInfo) {
        this._callback(itemInfo);
        this.hide();
    }

    onBtnConfirm () {
        let selects = [];
        for (let index = 0; index < this.plateListComps.length; index++) {
            const comp = this.plateListComps[index];
            let plates = comp.getSelect();
            selects = selects.concat(plates);
        }
        this._callback(selects);
        this.hide();
    }

    onTextChange () {
        let text = this.editbox.string;
        for (let index = 0; index < this.plateListComps.length; index++) {
            const comp = this.plateListComps[index];
            comp.setMatchText(text);
        }
    }

}
